package com.ziven.common.widget.test;

import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.common.widget.setting.BasePreferenceItem;
import com.ziven.common.widget.setting.ClickPreferenceItem;
import com.ziven.common.widget.setting.DialogPreferenceItem;
import com.ziven.common.widget.setting.MenuPreferenceItem;
import com.ziven.common.widget.setting.PreferenceItemsFactory;
import com.ziven.common.widget.setting.RedPointPreferenceItem;
import com.ziven.common.widget.setting.SwitchPreferenceItem;
import com.ziven.common.widget.value.Title;
import com.ziven.easygo.util.LogHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ziven
 */
public class PreferenceItemsFactoryImp implements PreferenceItemsFactory {

    public static final String TEST_RED_POINT = "test_red_point";
    private PreferenceItemsFactory.CallGetPreferenceItems call;

    @NonNull
    @Override
    public List<BasePreferenceItem> getPreferenceItems() {

        List<BasePreferenceItem> list = new ArrayList<>();

        //红点+Toast测试
        if(WidgetUtils.getSp().getBoolean(TEST_RED_POINT, false)) {
            list.add(new RedPointPreferenceItem().accept(
                    my -> ((RedPointPreferenceItem) my)
                            .setToast("我是Toast")
                            .setId(TEST_RED_POINT)
                            .setSubTitle2(Title.title("我是副标题1"))
                            .title = Title.title("红点测试")));
        }

        //SubTitle1+Fragment测试
        ClickPreferenceItem item = new ClickPreferenceItem().accept(
                my -> my.setFragment(TestSettingFragment.class.getName())
                        .setTitle(Title.title("Fragment测试"))
                        .setSubTitle1(Title.title("我是副标题1")));
        list.add(item);

        //SubTitle2+Dialog测试
        DialogPreferenceItem item1 = new DialogPreferenceItem().accept(
                my -> my.setTitle("我是Dialog")
                        .setMessage("我是Dialog的信息")
                        .setNegativeButton("取消")
                        .setPositiveButton("确定")
//                        .setNeutralButton("好的")
                        .setListener((dialog, which)
                                -> LogHelper.of("DialogPreferenceItemTest")
                                .join(dialog)
                                .join(which)
                                .print()));
        list.add(item1.accept(my -> {
            my.title = Title.title("Dialog测试");
            my.subTitle2 = Title.title("我是副标题2");
        }));

        //Menu测试
        MenuPreferenceItem item2 = new MenuPreferenceItem().accept(
                my -> my.menu("我是菜单1")
                        .menu("我是菜单2")
                        .subMenu(new MenuPreferenceItem.TypeMenu("我是菜单3").accept(
                                my1 -> my1
                                        .subTitle("我是子菜单菜单1")
                                        .subTitle("我是子菜单菜单2")
                                        .subTitle("我是子菜单菜单3")
                                        .subTypeMenu(new MenuPreferenceItem.TypeMenu("我是子菜单菜单4").setItemId(101))
                        ))
                        .menu("我是菜单4")
                        .listener(value -> {
                            LogHelper.of("MenuPreferenceItemTest")
                                    .join("click")
                                    .join(value)
                                    .print();
                            return false;
                        })
                        .listener(value -> {
                            LogHelper.of("MenuPreferenceItemTest")
                                    .join("dismiss")
                                    .join(value)
                                    .print();
                        }));
        list.add(item2.accept(my -> my
                .setTitle(Title.title("Menu测试"))
                .setId("test_menu_id")));

        //开关测试
        list.add(new SwitchPreferenceItem()
                .setTitle(Title.title("开关测试"))
                .setSubTitle1(Title.title("我是副标题1"))
                .setId("test_switch_button_1"));

        //关联开关测试
        list.add(new SwitchPreferenceItem()
                .setTitle(Title.title("关联开关测试"))
                .setSubTitle1(Title.title("我是副标题1"))
                .setId("test_switch_button_2")
                .setRelatedIds("test_switch_button_1"));

        return list;
    }

    @Override
    public boolean onPreferenceItemClick(BasePreferenceItem data, int position, @NonNull View view) {
        data.onItemClick(data, position, view);
        if(data instanceof RedPointPreferenceItem && call != null) {
            call.call();
        }
        return true;
    }

    @Override
    public void setCallGetPreferenceItems(CallGetPreferenceItems call) {
        this.call = call;
    }
}
